﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class VSDDAT
    {

        string TR = "", KZ = "", DR = "", SDR = "", VR = "";
        public static readonly VSDDAT SD22 = new VSDDAT(); 
        private DataRow _dwKEY;
        public DataRow dwKEY
        {
            get { return _dwKEY; }
            set
            {
                _dwKEY = value;
                OnPropertyChanged("dwKEY", value);
            }
        }
        VTXTIND TXT;
        private bool _IsSDDAT; public bool IsSDDAT { get { return _IsSDDAT; } set { _IsSDDAT = value; OnPropertyChanged("IsSDDAT", value); } }
        private string _SDSA; public string SDSA { get { return _SDSA; } set { _SDSA = value; OnPropertyChanged("SDSA", value); } }
        private string _SDES; public string SDES { get { return _SDES; } set { _SDES = value; OnPropertyChanged("SDES", value); } }
        private string _CGRP; public string CGRP { get { return _CGRP; } set { _CGRP = value; OnPropertyChanged("CGRP", value); } }
        private string _AUFTYPE; public string AUFTYPE { get { return _AUFTYPE; } set { _AUFTYPE = value; OnPropertyChanged("AUFTYPE", value); } }

        #region====0516===========
          

        public void QuerySDDAT(DataRow dwInput)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData = null;
            try
            {
                dwData = dwInput;

                if (dwData == null)
                {
                    dwData = SetData.Tables["SDDAT"].NewRow(); 
                    dwData["SDSA"] = _SDSA;
                    dwData["SDKEY"] = " AND SDKEY='00" + Helpers.Helper.Left(KEY.PadRight(21), 21) + "V'";

                }
                if (SDSA == "0014")
                {
                    if (KEY.Trim().Length < 2)
                    {
                        MessageBox.Show("必须输入两个以上字符");
                        return;
                    }
                    dwData["SDKEY"] = " AND SDKEY LIKE '" + KEY + "%" + LANG + "'";
                    SResult = QuerySDDAT(dwData, 14);
                }
                else
                {
                    if (KEY.Trim() == "")
                    {

                        SResult = QuerySDDAT(dwData, -1);
                    }
                    else
                    {
                        SResult = QuerySDDAT(dwData, Convert.ToInt16(_SDSA));
                    }
                }
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    row["DES"] = Helpers.Helper.GetGB2312Value(row["DES"].ToString());
                    row["SDES"] = Helpers.Helper.GetGB2312Value(row["SDES"].ToString());
                }
                //SResult.Tables[0].TableName = TableName;
                if (SetData.Tables.Contains(TableName))
                {
                    SetData.Tables[TableName].Clear();
                }
                //SReturn.Merge(SResult);
                SetData.Merge(SResult);
                SetData.AcceptChanges();
             }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void DeleteSDDAT(DataRow dwInput)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData = null;
            try
            {
                dwData = dwInput;
                if (dwData == null)
                {
                    MessageBox.Show("请选择需要删除的记录");
                    return;
                }
                DeleteSDDAT(dwData, 1);
                if (SetData.Tables[TableName].Rows.Count > 1)
                {
                    string filter = " VAFMANDANT='" + dwData["VAFMANDANT"].ToString() + "' AND VAFKOLL='" + dwData["VAFKOLL"].ToString() + "' AND VAFART ='" + dwData["VAFART"].ToString() + "' AND VAFFARBE='" + dwData["VAFFARBE"].ToString() + "'";
                    DataRow[] rows = SetData.Tables[TableName].Select(filter);
                    foreach (DataRow row in rows)
                    {
                        row.Delete();
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void SaveSDDAT(DataRow dwInput)
        {

            try
            {

                if (dwInput == null)
                {
                    MessageBox.Show("请选择需要保存的记录");
                }
                SaveSDDAT(dwInput, app.company);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void CopySDDAT(DataRow dwInput)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData = null;
            try
            {
                if (SelectedItem == null)
                    return;
                DataRow dwMain = ((DataRowView)SelectedItem).Row;
                if (dwMain == null)
                {
                    MessageBox.Show("请选择需要参考的记录");
                    return;
                }
                dwData = Convert2SDDAT(dwMain, app.company, true);
                dwData["VAFKOLL"] = dwInput["VAFKOLL"];
                dwData["VAFART"] = dwInput["VAFART"];
                dwData["VAFFARBE"] = dwInput["VAFFARBE"];
                dwData["VAFBEZ"] = dwInput["VAFBEZ"];
                DataRow dwNew = SetData.Tables["SDDAT"].Rows.Add();
                dwNew.ItemArray = dwData.ItemArray;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        #endregion
       
        public string GETNEXT(string _SDSA, string _KEY, string _AUFTYPE, int increase = 1)
        {
            return GETNEXT(_SDSA, _KEY, _AUFTYPE, increase, 6);
        }
        public string GETNEXT(string _SDSA, string _KEY, string _AUFTYPE, int increase, int length)
        {
            return GETNEXT(_SDSA, _KEY, _AUFTYPE, increase, length, 8);
        }
        
        public string GETNEXT(string _SDSA, string _KEY, string _AUFTYPE, int increase, int length, int keylength)
        {
            string VALUE = "";
            try
            {
                SetUp(_SDSA, ref TR, ref KZ, ref DR, ref SDR, ref VR);
                string num = "";

                dwData = TBMain.Clone().NewRow();
                dwData["SDSA"] = _SDSA;

                dwData["SDKEY"] = " AND SDKEY='" + ("00" + _AUFTYPE.PadRight(keylength).Substring(0, keylength) + Helper.Left(_KEY.PadRight(2), 2)).PadRight(22) + "00'";
                SResult = QuerySDDAT(dwData, Convert.ToInt16(_SDSA));
                MergeData(SResult);
                if (SResult.Tables[0].Rows.Count == 0)
                {
                    Insert(_SDSA, _KEY, _AUFTYPE, keylength);
                    SResult = QuerySDDAT(dwData, Convert.ToInt16(_SDSA));
                }
                MergeData(SResult);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    KEY = row["KEY"].ToString();
                    string daten = row["SDDATEN"].ToString();
                    VALUE = Helpers.Helper.Right("0000000000" + (Convert.ToDouble(daten.Substring(53 - length, length)) + increase).ToString(), length);
                    row["VALUE"] = VALUE;
                    row["SDDATEN"] = daten.Substring(0, 53 - length) + VALUE + daten.Substring(53);
                    UpdateSDDAT(row);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return VALUE;
        }
        public void RESETSEQ(string _SDSA, string _KEY, string _AUFTYPE, int start)
        {
            RESETSEQ(_SDSA, _KEY, _AUFTYPE, start.ToString());
        }
        public void RESETSEQ(string _SDSA, string _KEY, string _AUFTYPE, double start)
        {
            RESETSEQ(_SDSA, _KEY, _AUFTYPE, start.ToString());
        }
        public void RESETSEQ(string _SDSA, string _KEY, string _AUFTYPE, string start, int length = 6)
        {
            string VALUE = "";
            try
            {
                SetUp(_SDSA, ref TR, ref KZ, ref DR, ref SDR, ref VR);
                string num = "";

                dwData = TBMain.Clone().NewRow();
                dwData["SDSA"] = _SDSA;
                dwData["SDKEY"] = " AND SDKEY ='00" + _AUFTYPE.PadRight(8).Substring(0, 8) + Helper.Left(_KEY.PadRight(2), 2) + "          00'";
                SResult = QuerySDDAT(dwData, Convert.ToInt16(_SDSA));
                MergeData(SResult);
                if (SResult.Tables[0].Rows.Count == 0)
                {
                    Insert(_SDSA, _KEY, _AUFTYPE,8);
                    SResult = QuerySDDAT(dwData, Convert.ToInt16(_SDSA));
                }
                MergeData(SResult);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    KEY = row["KEY"].ToString();
                    VALUE = Helpers.Helper.Right("0000000000000000000000000" + start.ToString(), length);
                    string daten = row["SDDATEN"].ToString();
                    row["SDDATEN"] = daten.Substring(0, 53 - length) + VALUE + daten.Substring(53);
                    UpdateSDDAT(row);
                    //row["VALUE"] = VALUE;
                    //Save(row);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void Insert(string _SDSA, string _KEY, string _AUFTYPE,int keylength)
        {
            DataRow dwData;
            dwData = TBMain.NewRow();
            dwData["SDSA"] = _SDSA;
            /*
            string[] str = KZ.Split(',');
            int idx1 = Convert.ToInt16(str[0]);
            int idx2 = Convert.ToInt16(str[1]);
            string SDKEY = dwSD["SDKEY"].ToString();
            SDKEY = Helper.Left(SDKEY, idx1 - 1) + Helper.Left(dwSD["KEY"].ToString().PadRight(idx2), idx2) + SDKEY.Substring(idx1 + idx2 - 1);
            */
            dwData["SDKEY"] = ("00" + _AUFTYPE.PadRight(keylength).Substring(0, keylength) + Helper.Left(_KEY.PadRight(2), 2)).PadRight(22) + "00";
            dwData["KEY"] = Helpers.Helper.Left(_KEY.PadRight(2), 2);
            dwData["DES"] = _AUFTYPE;
            dwData["SDES"] = _AUFTYPE;
            dwData["VALUE"] = "0000000";
            dwData["SDDATEN"] = "ITXAUTOINSERT       000000000010000099999900000000001E  000000019980202    000000000                                                           00000000INTEX   201012210001*";
            switch (_AUFTYPE)
            {
                case "KUNDNR":
                    SResult = QuerySDDAT(dwData, -4);
                    foreach (DataRow row in SResult.Tables[0].Rows)
                    {
                        dwData["VALUE"] = Helper.Right(row["KUNDNR"].ToString(), 6);
                    }
                    break;
                case "VKAUFTR":
                    SResult = QuerySDDAT(dwData, -5);
                    foreach (DataRow row in SResult.Tables[0].Rows)
                    {
                        dwData["VALUE"] = Helper.Right("000000" + row["VKAUFTR"].ToString(), 6);
                    }
                    break;
                case "LIEFDAT":
                    SResult = QuerySDDAT(dwData, -6);
                    foreach (DataRow row in SResult.Tables[0].Rows)
                    {
                        dwData["VALUE"] = Helper.Right("000000" + row["LSLIEFNR"].ToString(), 6);
                    }
                    break;
            }
            Save(dwData);
        }

        public void Save(DataRow dwSD)
        {

            if (dwSD == null)
                return;
            string[] str = KZ.Split(',');
            int idx1 = Convert.ToInt16(str[0]);
            int idx2 = Convert.ToInt16(str[1]);
            string SDKEY = dwSD["SDKEY"].ToString();

            string SDDATEN = dwSD["SDDATEN"].ToString();
            SDKEY = Helper.Left(SDKEY, idx1 - 1) + Helper.Left(dwSD["KEY"].ToString().PadRight(idx2), idx2) + SDKEY.Substring(idx1 + idx2 - 1);
            dwSD["SDKEY"] = SDKEY;
            if (TR.Length > 1)
            {
                str = TR.Split(',');
                idx1 = Convert.ToInt16(str[0]);
                idx2 = Convert.ToInt16(str[1]);
                SDDATEN = Helper.Left(SDKEY, idx1 - 1) + Helper.Left(dwSD["AUFTYP"].ToString().PadRight(idx2), idx2) + SDDATEN.Substring(idx1 + idx2 - 1);

            }
            str = DR.Split(',');
            idx1 = Convert.ToInt16(str[0]);
            idx2 = Convert.ToInt16(str[1]);
            SDDATEN = Helper.Left(SDDATEN, idx1 - 1) + Helper.Left(dwSD["DES"].ToString().PadRight(idx2), idx2) + SDDATEN.Substring(idx1 + idx2 - 1);
            str = SDR.Split(',');
            idx1 = Convert.ToInt16(str[0]);
            idx2 = Convert.ToInt16(str[1]);
            SDDATEN = Helper.Left(SDDATEN, idx1 - 1) + Helper.Left(dwSD["SDES"].ToString().PadRight(idx2), idx2) + SDDATEN.Substring(idx1 + idx2 - 1);

            str = VR.Split(',');
            idx1 = Convert.ToInt16(str[0]);
            idx2 = Convert.ToInt16(str[1]);
            SDDATEN = Helper.Left(SDDATEN, idx1 - 1) + Helper.Left(dwSD["VALUE"].ToString().PadRight(idx2), idx2) + SDDATEN.Substring(idx1 + idx2 - 1);
            dwSD["SDDATEN"] = SDDATEN.Substring(0,151)+ Helpers.Helper.Left(app.User.PadRight(8), 8) + Helpers.Helper.Left(INTEXDBC.GetDBDATE().PadRight(8), 8) + "0000*";
            SaveSDDAT(dwSD, app.company);
        }

        public void SetUp(string SDSA, ref string TR, ref string KZ, ref string DR, ref string SDR, ref string VR)
        {
            switch (SDSA)
            {
                case "0012":
                    TR = "";
                    KZ = "3,4";
                    DR = "2,28";
                    SDR = "52,10";
                    VR = "68,2";
                    break;
                case "0013":
                    TR = "";
                    KZ = "3,6";
                    DR = "2,40";
                    SDR = "52,10";
                    VR = "62,8";
                    break;
                case "0015":
                    TR = "";
                    KZ = "3,1";
                    DR = "2,28";
                    SDR = "52,10";
                    VR = "68,2";
                    break;
                case "0022":
                    TR = "3,8";
                    KZ = "11,2";
                    DR = "74,2";
                    SDR = "1,19";
                    VR = "48,6";
                    break;
                case "8885":
                    TR = "3,8";
                    KZ = "11,2";
                    DR = "74,2";
                    SDR = "1,19";
                    VR = "44,10";
                    break;
                case "0109":
                    TR = "";
                    KZ = "3,15";
                    DR = "2,28";
                    SDR = "52,10";
                    VR = "62,8";
                    break;

                case "0149":
                    TR = "";
                    KZ = "3,2";
                    DR = "2,22";
                    SDR = "52,10";
                    VR = "45,7";
                    break;
                case "0150":
                    TR = "";
                    KZ = "3,2";
                    DR = "2,22";
                    SDR = "52,10";
                    VR = "45,7";
                    break;
                case "0114":
                    TR = "";
                    KZ = "3,2";
                    DR = "2,22";
                    SDR = "52,10";
                    VR = "62,8";
                    break;
                case "0178":
                    TR = "";
                    KZ = "3,3";
                    DR = "2,22";
                    SDR = "52,10";
                    VR = "62,8";
                    break;
                case "0181":
                    TR = "";
                    KZ = "3,3";
                    DR = "2,30";
                    SDR = "39,1";
                    VR = "40,12";
                    break;
                case "0187":
                    TR = "";
                    KZ = "3,2";
                    DR = "2,30";
                    SDR = "52,10";
                    VR = "68,2";
                    break;
                case "0192":
                    TR = "";
                    KZ = "3,3";
                    DR = "2,22";
                    SDR = "52,10";
                    VR = "62,8";
                    break;
                case "0196":
                    TR = "";
                    KZ = "3,3";
                    DR = "2,30";
                    SDR = "52,10";
                    VR = "62,8";
                    break;
                case "0201":
                    TR = "";
                    KZ = "3,3";
                    DR = "2,30";
                    SDR = "52,10";
                    VR = "62,8";
                    break;
                case "0224":
                case "0228":
                    TR = "";
                    KZ = "3,8";
                    DR = "2,28";
                    SDR = "52,10";
                    VR = "68,2";
                    break;
                case "0231":
                    TR = "";
                    KZ = "3,15";
                    DR = "2,28";
                    SDR = "52,10";
                    VR = "68,2";
                    break;
                case "0245":
                case "0249":
                    TR = "";
                    KZ = "3,2";
                    DR = "2,28";
                    SDR = "52,10";
                    VR = "62,20";
                    break;

                case "0208":
                case "0209":
                case "0241":
                case "0243":
                case "0253":
                case "0255":
                    TR = "";
                    KZ = "3,3";
                    DR = "2,22";
                    SDR = "52,10";
                    VR = "62,8";
                    break;
                case "9314":
                case "0267":
                    TR = "";
                    KZ = "3,3";
                    DR = "2,28";
                    SDR = "52,10";
                    VR = "62,20";
                    break;
                case "0352":
                    TR = "";
                    KZ = "3,10";
                    DR = "2,50";
                    SDR = "52,10";
                    VR = "68,60";
                    break;
                case "0477":
                    TR = "";
                    KZ = "3,15";
                    DR = "2,50";
                    SDR = "52,10";
                    VR = "62,8";
                    break;
                case "0561":
                    TR = "";
                    KZ = "3,15";
                    DR = "2,50";
                    SDR = "52,10";
                    VR = "70,8";
                    break;
                case "0622":
                    TR = "";
                    KZ = "3,2";
                    DR = "2,29";
                    SDR = "52,10";
                    VR = "68,2";
                    break;
                case "0100": 
                case "0414":
                case "0439":
                case "0433":
                case "0434":
                case "0435":
                case "0436":
                case "0437":
                case "0804":
                case "9158":
                case "9315":
                case "9316":
                case "9317":
                case "9318":
                case "9319":
                case "9321":
                case "8874":
                case "8876":
                case "8877":
                case "8880":
                case "8881":
                case "8882":
                case "9200":
                case "9320":
                case "9325":
                case "9343":
                    TR = "";
                    KZ = "3,1";
                    DR = "2,28";
                    SDR = "52,10";
                    VR = "68,2";
                    break;
                case "0168":
                case "0041":
                case "0354":
                case "0514":
                case "0280":
                case "0706":
                case "8875":
                case "8878":
                case "8879":
                case "8884":
                case "0412":
                case "0413":
                case "0427":
                case "9324":
                case "9322":
                case "8886":
                case "8872":
                    TR = "";
                    KZ = "3,2";
                    DR = "2,28";
                    SDR = "52,10";
                    VR = "68,2";
                    break;

                case "0275":
                case "0460":
                case "0424":
                case "0450":
                case "0451":
                case "0453":
                case "0455":
                case "0665":
                case "0457":
                case "0458":
                case "0674":
                case "0676":
                case "0678":
                case "0680":
                case "8873":
                    TR = "";
                    KZ = "3,3";
                    DR = "2,28";
                    SDR = "52,10";
                    VR = "68,2";
                    break; 
                case "8883":
                    TR = "";
                    KZ = "3,8";
                    DR = "2,28";
                    SDR = "52,10";
                    VR = "62,8";
                    break;
                case "0252":
                case "8887":
                case "8888":
                    TR = "";
                    KZ = "3,4";
                    DR = "2,28";
                    SDR = "52,10";
                    VR = "68,2";
                    break;
            }
        }
         public void SetUp(string _SDSA)
        {
            SetUp(_SDSA, ref TR, ref KZ, ref DR, ref SDR, ref VR); 
        }
    } 
}
